-- get the addon namespace
local addon, ns = ...

-- get the config values
local cfg = ns.cfg

-- get the library
local lib = ns.lib
local db = ns.db

lib.classes["MONK"] = {}
local t,s,n
local globalwait;
lib.classes["MONK"][1] = function()

	cfg.talentname = "JS"

	lib.AddSpell("Palm",{100787}) --Tiger Palm
	lib.AddAura("Power",125359,"buff","player") -- Tiger Power 
	lib.AddSpell("Kick",{100784}) --Blackout Kick 幻灭
	lib.AddSpell("Blossoms",{124336}) --Path of Blossoms
	lib.AddSpell("Keg",{121253},nil,nil,40) --Keg Smash 醉酿
	lib.AddSpell("Haze",{115180}) --Dizzying Haze 迷醉
--	lib.AddAura("Haze",116330,"debuff","target") -- Dizzying Haze (123727 - Keg, 116330 - Dizzying Haze)
	lib.AddAuras("Haze",{123727,116330},"debuff","target") -- Dizzying Haze (123727 - Keg, 116330 - Dizzying Haze)
	lib.AddAura("Weakened",115798,"debuff","target","any") -- Weakened Blows
	lib.AddSpell("Breath",{115181}) -- Breath of Fire
	lib.AddAura("Breath",123725,"debuff","target") -- Breath of Fire
	lib.AddSpell("Sphere",{124081}) -- Zen Sphere
	lib.AddAura("Sphere",124081,"buff","player") -- Zen Sphere
	lib.AddSpell("Expel",{115072},nil,nil,40) -- Expel Harm 移花接木
	lib.AddSpell("Guard",{115295}) -- Guard 金钟罩
	lib.AddSpell("Death",{115080}) -- Touch of Death
	lib.AddSpell("Spin",{101546}) -- Spinning Crane Kick 神鹤
	lib.AddAura("Spin",101546,"buff","player") -- Spinning Crane Kick
	lib.AddAura("Shuffle",115307,"buff","player") -- Shuffle 酒醒
	lib.AddSpell("Pmj",{115308}) -- 飘渺酒
	lib.AddAura("Pmj",128939,"buff","player") -- 飘渺酒
	lib.AddSpell("ZQB",{115098}) --
	lib.AddSpell("Hxj",{119582}) --
	lib.AddSpell("Jab",{100780,115693,115695,115687,115698,108557},nil,nil,40)

	lib.AddAura("zq_1",124275,"debuff","player") -- 飘渺酒
	lib.AddAura("zq_2",124274,"debuff","player") -- 飘渺酒
	lib.AddAura("zq_3",124273,"debuff","player") -- 飘渺酒
	
	cfg.plistdps = {}
--	table.insert(cfg.plistdps,"Death")
--	table.insert(cfg.plistdps,"Expel")
	table.insert(cfg.plistdps,"PARAMS")
	--table.insert(cfg.plistdps,"Guard")
	table.insert(cfg.plistdps,"Hxj")
	table.insert(cfg.plistdps,"Palm_Power")
	table.insert(cfg.plistdps,"Keg")
	table.insert(cfg.plistdps,"Kick")
	table.insert(cfg.plistdps,"Breath")
--	table.insert(cfg.plistdps,"Sphere_noSphere")
--	table.insert(cfg.plistdps,"Breath_Haze")
	table.insert(cfg.plistdps,"Jab")
	table.insert(cfg.plistdps,"ZQB")
	table.insert(cfg.plistdps,"Palm")
	table.insert(cfg.plistdps,"end")
	
	cfg.plistaoe = {}
--	table.insert(cfg.plistaoe,"Expel")
	table.insert(cfg.plistaoe,"PARAMS")
	table.insert(cfg.plistaoe,"Guard")
	table.insert(cfg.plistaoe,"Hxj")
	table.insert(cfg.plistaoe,"Palm_Power")
	table.insert(cfg.plistaoe,"Keg")
	table.insert(cfg.plistaoe,"Kick")
	table.insert(cfg.plistaoe,"Breath")
--	table.insert(cfg.plistdps,"Sphere_noSphere")
--	table.insert(cfg.plistdps,"Breath_Haze")
	table.insert(cfg.plistaoe,"ZQB")
	table.insert(cfg.plistaoe,"Spin")
	table.insert(cfg.plistaoe,"end")
	
	cfg.plistrange = {}
--	table.insert(cfg.plistrange,"Expel")
	table.insert(cfg.plistrange,"Guard")
	table.insert(cfg.plistrange,"Sphere_noSphere")
	table.insert(cfg.plistrange,"Breath_Haze")
	table.insert(cfg.plistrange,"Keg")
	table.insert(cfg.plistrange,"Jab")
	table.insert(cfg.plistrange,"Blossoms")
	table.insert(cfg.plistrange,"end")
	
	cfg.plist=cfg.plistdps

	cfg.gcd = "Palm"
	
	
	--[[			for i=1,40 do
				local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId=UnitDebuff("target",i)
				if name then print(name.." = "..spellId.."("..count..")") end
			end]]
			
	cfg.case = {

		["PARAMS"] = function()
			globalwait = lib.GetSpellCD(cfg.gcd,true)
			if cfg.casting and cfg.castEnd - cfg.now > globalwait then
				globalwait = cfg.castEnd - cfg.now
			end
			return nil
		end,
		["Guard"] = function()
--[[			for i=1,40 do
				local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId=UnitBuff("player",i)
				if name then print(name.." = "..spellId.."("..count..")") end
			end]]
			if cfg.target=="normal" then return nil end
			return lib.SimpleCDCheck("Guard",lib.GetAura({"Spin"}))
		end,
		["Hxj"] = function()
			if lib.GetAura({"zq_3"}) > 0 or ((lib.GetAura({"Shuffle"}) >= 6 or cfg.chi >= 2) and ( (UnitStagger('player') > cfg.phealthmax * 0.4) or (cfg.health <= 70 and lib.GetAura({"zq_2"}) > 0))) then
				return lib.SimpleCDCheck("Hxj")
			end
			return nil
		end,
		["Haze_15sec"] = function()
			return lib.EnergyCDCheck("Haze",13-lib.GetLastCast(lib.GetSpellName("Haze")))
		end,
		["Expel"] = function()
			if cfg.phealthperc<=90 then
				return lib.EnergyCDCheck("Expel")
			end
			return nil
		end,
		["Sphere_noSphere"] = function()
			if lib.GetAura({"Spin"})>0 and lib.GetSpellCost("Sphere")<=cfg.chi then
				return lib.SimpleCDCheck("Sphere",lib.GetLargestNumber({lib.GetAura({"Sphere"})-1},lib.GetAura({"Spin"})),true)
			end
			return lib.SimpleCDCheck("Sphere",lib.GetAura({"Sphere"})-1)
		end,
		["Blossoms"] = function()
			if cfg.chi==cfg.maxchi then
				return lib.EnergyCDCheck("Blossoms")
			end
			return nil
		end,
		["Sphere"] = function()
			if cfg.chi<(lib.GetSpellCost("Sphere")) then return nil end
			if lib.GetAura({"Spin"})>0 then
				return lib.SimpleCDCheck("Sphere",lib.GetAura({"Spin"}),true)
			end
			return lib.SimpleCDCheck("Sphere",lib.GetAura({"Spin"}))
		end,
		["Breath"] = function()
			if lib.GetAuras("Haze")>lib.GetSpellCD("Breath") and lib.GetSpellCD('Keg') >= 3.5 + globalwait and lib.GetAura({"zq_3"}) == 0 and lib.GetAura({"zq_2"}) == 0 and cfg.chi >= 4 then
				return lib.SimpleCDCheck("Breath",lib.GetAura({"Breath"})-1)
			end
			return nil
		end,
		["Breath_Haze"] = function()
			if lib.GetAuras("Haze")>lib.GetSpellCD("Breath") then
				if lib.GetAura({"Spin"})>0 and lib.GetSpellCost("Breath")<=cfg.chi then
					return lib.SimpleCDCheck("Breath",lib.GetLargestNumber({lib.GetAura({"Breath"})-1},lib.GetAura({"Spin"})),true)
				end
				return lib.SimpleCDCheck("Breath",lib.GetAura({"Breath"})-1)
			end
			return nil
		end,
		["Palm"] = function()
			return lib.SimpleCDCheck("Palm")
		end,
		["Palm_Power"] = function()
			if lib.GetAura({"Power"}) <= 2 + globalwait then
				return lib.SimpleCDCheck("Palm")
			end
			return nil
		end,
		["Kick"] = function()
			if (cfg.chi>2 and lib.GetAura({"Shuffle"}) <= 2 + globalwait) then
				return lib.SimpleCDCheck("Kick")
			elseif cfg.chi > cfg.maxchi-2  then
				if lib.GetAura({"Shuffle"}) >= 3.5 + globalwait then
					if lib.GetAuras("Haze")>lib.GetSpellCD("Breath") then
						return lib.SimpleCDCheck("Breath",lib.GetAura({"Breath"})-1)
					end
				end
				return lib.SimpleCDCheck("Kick")
			end
			return nil
		end,
--[[		["Palm"] = function()
			if cfg.chi>2 then
				return lib.SimpleCDCheck("Palm")
			end
			return nil
		end,]]
		["Keg"] = function()
			if cfg.chi>cfg.maxchi-1 then return nil end
			return lib.SimpleCDCheck("Keg")
		end,
		["Jab_15sec"] = function()
			if cfg.chi>=cfg.maxchi-2 then return nil end
			if lib.TimeEnergy(lib.GetSpellCD("Keg"))>=(lib.GetSpellCost("Keg")+lib.GetSpellCost("Jab")) then
				return lib.EnergyCDCheck("Jab",15-lib.GetLastCast(lib.GetSpellName("Jab")))
			end
			return nil
		end,
		["Jab"] = function()
			if cfg.chi>cfg.maxchi-1 then return nil end
			if cfg.power > 80 or lib.TimeEnergy(lib.GetSpellCD("Keg"))>=(lib.GetSpellCost("Keg")+lib.GetSpellCost("Jab")) then
				if lib.GetSpellCD("Expel") <= globalwait then
					return lib.SimpleCDCheck("Expel")
				else
					return lib.SimpleCDCheck("Jab")
				end
			end
			return nil
		end,
		["ZQB"] = function()
			return lib.SimpleCDCheck("ZQB");
		end,
		["Spin"] = function()
			if cfg.chi>=cfg.maxchi-1 then return nil end
			if lib.TimeEnergy(lib.GetSpellCD("Keg"))>=(lib.GetSpellCost("Keg")+lib.GetSpellCost("Spin")) then
				return lib.EnergyCDCheck("Spin",lib.GetAura({"Spin"}))
			end
			return nil
		end,
	}
	
	cfg.mode = "dps"
	lib.onclick = function()
		if cfg.mode == "dps" then
			cfg.mode = "aoe"
			cfg.plist=cfg.plistaoe
			cfg.Update=true
		else
			cfg.mode = "dps"
			cfg.plist=cfg.plistdps
			cfg.Update=true
		end
	end
	
--	cfg.spells_aoe={"Spin"}
--	cfg.spells_single={"Palm","Kick"}
--	cfg.spells_range={"Blossoms"}
	return true
end

local lastTarget = nil
lib.classes["MONK"][2] = function()

	cfg.talentname = "WSZW"

	lib.AddSpell("GRJ",{100780,115693,115695,115687,115698,108557})
	lib.AddSpell("MHZ",{100787}) --Tiger Palm
	lib.AddSpell("HMT",{100784}) --
	lib.AddSpell("XRDST",{107428}) --
	
	lib.AddSpell("FW",{115175}) --
	lib.AddSpell("YY",{124682}) --
	lib.AddSpell("ST",{116694}) --
	lib.AddSpell("ZQB",{115098}) --
	lib.AddSpell("YHJM",{115072}) --
	lib.AddSpell("SHYXT",{101546}) --
	
	lib.AddSpell("FS",{115151}) --
	lib.AddSpell("ZHY",{116670}) --
	lib.AddSpell("LG",{115175}) --
	
	lib.AddSpell("LHZS",{115178}) --
	lib.AddSpell("FLC",{123761}) --
	
	
	lib.AddAura("B_YY",124682,'buff','target') --
	lib.AddAura("B_HMT",127722,'buff','player') --
	lib.AddAura("B_HLZW",118674,'buff','player') --
	lib.AddAura("B_MHZ",125359,'buff','player') --
	lib.AddAura("B_SNSQ",139597,'buff','player') --
	lib.AddAura("B_FLC",115867,'buff','player') --
	cfg.healingFlag = true
	
	cfg.gcd = "LHZS"
	
	cfg.plistfulldps = {}
	table.insert(cfg.plistfulldps,"PARAMS")
	table.insert(cfg.plistfulldps,"FLC")
	table.insert(cfg.plistfulldps,"XRDST")
	table.insert(cfg.plistfulldps,"MHZ")
	table.insert(cfg.plistfulldps,"HMT")
	table.insert(cfg.plistfulldps,"HMT_FULL")
	table.insert(cfg.plistfulldps,"ZQB")
	table.insert(cfg.plistfulldps,"YHJM")
	table.insert(cfg.plistfulldps,"ST")
	table.insert(cfg.plistfulldps,"GRJ")
	
	table.insert(cfg.plistfulldps,"end")
	
	
	cfg.plistdps = {}
	--[[
	table.insert(cfg.plistdps,"HealingTarget")
	table.insert(cfg.plistdps,"ST")
	table.insert(cfg.plistdps,"ZQB")
	table.insert(cfg.plistdps,"YY")
	table.insert(cfg.plistdps,"FW")
	]]
	table.insert(cfg.plistdps,"PARAMS")
	table.insert(cfg.plistdps,"FLC")
	table.insert(cfg.plistdps,"XRDST")
	table.insert(cfg.plistdps,"MHZ")
	table.insert(cfg.plistdps,"HMT")--
	table.insert(cfg.plistdps,"HMT_FULL")
	table.insert(cfg.plistdps,"YHJM")
	table.insert(cfg.plistdps,"ST")--
	table.insert(cfg.plistdps,"ZQB")
	table.insert(cfg.plistdps,"GRJ")
	table.insert(cfg.plistdps,"end")
	
	
	cfg.plistaoe = {}
	table.insert(cfg.plistaoe,"FLC")
	table.insert(cfg.plistaoe,"YHJM")
	table.insert(cfg.plistaoe,"ST")
	table.insert(cfg.plistaoe,"ZQB")
	--table.insert(cfg.plistaoe,"FS")
	--table.insert(cfg.plistaoe,"GRJ")
	table.insert(cfg.plistaoe,"SHYXT")
	table.insert(cfg.plistaoe,"HMT_AOE")
	table.insert(cfg.plistaoe,"MHZ")
	table.insert(cfg.plistaoe,"end")
	
	
	cfg.plist=cfg.plistdps
	
	cfg.case = {}
	cfg.case = {
		["PARAMS"] = function()
			globalwait = lib.GetSpellCD(cfg.gcd,true)
			if cfg.casting and cfg.castEnd - cfg.now > globalwait then
				globalwait = cfg.castEnd - cfg.now
			end
			RFC = 0
			return nil
			end,
		["YHJM"] = function ()
			if not UnitAffectingCombat('player') then
				return nil
			end
			
			if cfg.phealthmax-cfg.phealth > 3000 then
				return lib.SimpleCDCheck("YHJM")
			end
			return nil
		end,
		["FLC"] = function ()
			if not UnitAffectingCombat('player') then
				return nil
			end
			if lib.GetAura({"B_FLC"}) > 0 and cfg.power * 100 / cfg.powermax < 80 then
				return lib.SimpleCDCheck("FLC")
			end
			return nil
		end,
		["ZQB"] = function ()
			if not UnitAffectingCombat('player') then
				return nil
			end
			return lib.SimpleCDCheck('ZQB')
		end,
		["ST"] = function ()
			if not UnitAffectingCombat('player') then
				return nil
			end
			if lib.GetAuraNum("B_HLZW") >= 5 then
				return lib.EnergyCDCheck("ST")
			end
			return nil
		end,
		["FS"] = function ()
			if not UnitAffectingCombat('player') then
				return nil
			end
			return lib.EnergyCDCheck("FS")
		end,
		["GRJ"] = function ()
			if not UnitAffectingCombat('player') then
				return nil
			end
			return lib.EnergyCDCheck("GRJ")
		end,
		["MHZ"] = function ()
			if not UnitAffectingCombat('player') then
				return nil
			end
			if cfg.chi < RFC then
				return nil
			end
			if lib.GetAura({"B_MHZ"}) - globalwait <= 1 and cfg.chi >= 1 then
				return lib.SimpleCDCheck("MHZ")
			end
			return nil
		end,
		["HMT_FULL"] = function ()
			if not UnitAffectingCombat('player') then
				return nil
			end
			if cfg.chi >= 5 then
				return lib.SimpleCDCheck("HMT")
			end
			return nil
		end,
		["HMT"] = function ()
			if not UnitAffectingCombat('player') then
				return nil
			end
			if cfg.chi < RFC then
				return nil
			end
			if lib.GetAura({"B_HMT"}) - globalwait <= 1 and cfg.chi >= 2 then
				return lib.SimpleCDCheck("HMT")
			end
			return nil
		end,
		['XRDST'] = function()
			if not UnitAffectingCombat('player') then
				return nil
			end
			if lib.GetSpellCD('XRDST') - globalwait <= 0.5 then
				if cfg.chi < 2 then
					RFC = 2
				else
					return lib.SimpleCDCheck("XRDST",0,true)
				end
			end
			return nil
		end,
		["HMT_AOE"] = function ()
			if not UnitAffectingCombat('player') then
				return nil
			end
			if lib.GetAura({"B_QLZZ"}) - lib.GetSpellCD(cfg.gcd) <= 0 then
				if (cfg.chi >= 2 and lib.GetAura({"B_SNSQ"}) > 0) or cfg.chi >= 5 then
					return lib.SimpleCDCheck("HMT")
				else
					return lib.SimpleCDCheck("SHYXT")
				end
			end
			if cfg.chi >= 2 and lib.GetAura({"B_SNSQ"}) > 0 and lib.GetAura({"B_MHZ"}) >= 3 then
				return lib.SimpleCDCheck("HMT")
			end
			return nil
		end,
		
		["SHYXT"] = function()
			if not UnitAffectingCombat('player') then
				return nil
			end
			if lib.GetAura({"B_SNSQ"}) <= 0 then
				return lib.SimpleCDCheck("SHYXT",0,true)
			end
			return nil
		end,
	--[[
		["HealingTarget"] = function ()
			local target = 0
			local prefix = cfg.raidmode
			local tmhp = 101
			local hmhp = 101
			local dmhp = 101
			local tt,ht,dt = nil,nil,nil
			for i = 1 , GetNumGroupMembers() do
				if not UnitIsPlayer(prefix..i) or UnitIsDead(prefix..i) or not UnitInRange(prefix..i) then
					
				else
				local hp = UnitHealth(prefix..i) * 100 / UnitHealthMax(prefix..i)
				local role = UnitGroupRolesAssigned(prefix..i)
				if role == 'TANK' and hp < tmhp then
					tmhp = hp
					tt = i
				elseif role == 'HEALER' and hp < hmhp then
					hmhp = hp
					ht = i
				elseif (role == 'DAMAGER' or not role) and hp < dmhp then
					dmhp = hp
					tt = i
				end
				end
			end
			if IsInGroup() and not IsInRaid() then
				local hp = UnitHealth('player') * 100 / UnitHealthMax('player')
				if hp < hmhp or hp <= 40 then
					hmhp = hp
					ht = 0
				end
			end
			if ht == 0 and hmhp <= 30 and tmhp > hmhp then
				target = 0
			elseif tmhp <= 60 or (tmhp <= 95 and hmhp >= 40) then
				target = tt
			elseif hmhp <= 95 and dmhp >= 40 then
				target = ht
			elseif dmhp <= 95 then
				target = dt
			else
				if tmhp < 100 then
					target = tt
				elseif hmhp < 100 then
					target = ht
				elseif dmhp < 100 then
					target = dt
				end
			end
			--print(target)
			if target == nil then
				target = 0
			end
			local rt
			if target == 0 then
				rt = UnitName('player')
			else
				rt = UnitName(prefix..target)
			end
			if rt ~= UnitName('target') then
				lib.setControlValue(4,255,255,target/255)
			else
				lib.setControlValue(4,0,0,0)
			end
			return nil
		end,
		
		["FW"] = function ()
			if not UnitIsPlayer('target') or UnitIsDead('target') or not UnitInRange('target') then
				return nil
			end
			local hp = UnitHealth('target') * 100 / UnitHealthMax('target')
			endtime = select(6,UnitChannelInfo('player'))
			if cfg.health < 100 and (UnitChannelInfo('player') ~= cfg.spells["FW"].name or endtime/1000 <= GetTime() + lib.GetSpellCD(cfg.gcd) + 0.5) then
				return lib.SimpleCDCheck('FW')
			end
			return nil
		end,
		["ST"] = function ()
			if not UnitIsPlayer('target') or UnitIsDead('target') or not UnitInRange('target') then
				return nil
			end
			local hp = UnitHealth('target') * 100 / UnitHealthMax('target')
			if cfg.health <= 20 then
				endtime = select(6,UnitChannelInfo('player'))
				if UnitChannelInfo('player') ~= cfg.spells["FW"].name or endtime/1000 <= GetTime() + lib.GetSpellCD(cfg.gcd) + 0.5 then
					return lib.SimpleCDCheck('FW')
				else
					return lib.SimpleCDCheck('ST')
				end
			end
			return nil
		end,
		["ZQB"] = function ()
			if not UnitIsPlayer('target') or UnitIsDead('target') or not UnitInRange('target') then
				return nil
			end
			local hp = UnitHealth('target') * 100 / UnitHealthMax('target')
			if cfg.health <= 95 then
				return lib.SimpleCDCheck('ZQB')
			end
			return nil
		end,
		["YY"] = function ()
			if not UnitIsPlayer('target') or UnitIsDead('target') or not UnitInRange('target') then
				return nil
			end
			local hp = UnitHealth('target') * 100 / UnitHealthMax('target')
			if cfg.chi >= 3 and cfg.health <= 90 then
				endtime = select(6,UnitChannelInfo('player'))
				if UnitChannelInfo('player') ~= cfg.spells["FW"].name or endtime/1000 <= GetTime() + lib.GetSpellCD(cfg.gcd) + 0.5 then
					return lib.SimpleCDCheck('FW')
				else
					return lib.SimpleCDCheck('YY')
				end
			end
			return nil
		end,
		]]
	}
	cfg.gcd="GRJ"
	cfg.mode = "dps"
	lib.onclick = function()
		if cfg.mode == "dps" then
			cfg.mode = "aoe"
			cfg.plist=cfg.plistaoe
			cfg.Update=true
		else
			cfg.mode = "dps"
			cfg.plist=cfg.plistdps
			lib.changeDPSMode()
			cfg.Update=true
		end
	end
	
	return true
end
	

local RFC = 0
local XLZTTimer = 0
lib.classes["MONK"][3] = function()

	cfg.talentname = "WSTF"

	lib.AddSpell("GRJ",{100780,115693,115695,115687,115698,108557})
	lib.AddSpell("MHZ",{100787}) --Tiger Palm
	lib.AddSpell("HMT",{100784}) --
	lib.AddSpell("ZQB",{115098}) --
	lib.AddSpell("YHJM",{115072}) --
	lib.AddSpell("XRDST",{107428}) --
	lib.AddSpell("NLP",{113656}) --
	lib.AddSpell("BHXF",{123904}) --
	lib.AddSpell("XLZT",{101545}) --
	lib.AddSpell("XLZTR",{115057}) --
	lib.AddSpell("SYSD",{117952}) --
	lib.AddSpell("HYJ",{116740}) --
	lib.AddSpell("HNJ",{115288}) --
	lib.AddSpell("SHYXT",{101546}) --
	
	lib.AddAura("B_HNJ",115288,"buff","player") --
	lib.AddAura("B_MHZ",125359,"buff","player") -- Tiger Power
	lib.AddAura("T_HMT",116768,"buff","player") -- Tiger Power
	lib.AddAura("T_MHZ",118864,"buff","player") -- Tiger Power
	lib.AddAura("B_HYJ",125195,"buff","player") -- Tiger Power
	lib.AddAura("B_HYJ_D",116740,"buff","player") -- Tiger Power
	--lib.AddAura("B_MHZ_AOE",125359,"buff","player") -- Tiger Power
	
	cfg.plistfulldps = {}
	table.insert(cfg.plistfulldps,"RFC_Reset")
	table.insert(cfg.plistfulldps,"CDs")
	--table.insert(cfg.plistdps,"XLZTR")
	table.insert(cfg.plistfulldps,"HYJ")
	table.insert(cfg.plistfulldps,"HNJ")
	table.insert(cfg.plistfulldps,"XRDST")
	table.insert(cfg.plistfulldps,"MHZ_B")
	table.insert(cfg.plistfulldps,"NLP")
	table.insert(cfg.plistfulldps,"ZQB")
	table.insert(cfg.plistfulldps,"YHJM_RFC")
	table.insert(cfg.plistfulldps,"GRJ_RFC")
	table.insert(cfg.plistfulldps,"HMT_T")
	table.insert(cfg.plistfulldps,"MHZ_T")
	table.insert(cfg.plistfulldps,"HMT")
	table.insert(cfg.plistfulldps,"YHJM")
	table.insert(cfg.plistfulldps,"GRJ")
	--table.insert(cfg.plistdps,"XLZT")
	table.insert(cfg.plistfulldps,"GRJ_Force")
	table.insert(cfg.plistfulldps,"end")
	
	cfg.plistdps = {}
	table.insert(cfg.plistdps,"RFC_Reset")
	--table.insert(cfg.plistdps,"XLZTR")
	table.insert(cfg.plistdps,"HYJ")
	table.insert(cfg.plistdps,"HNJ")
	table.insert(cfg.plistdps,"XRDST")
	table.insert(cfg.plistdps,"MHZ_B")
	table.insert(cfg.plistdps,"NLP")
	table.insert(cfg.plistdps,"ZQB")
	table.insert(cfg.plistdps,"YHJM_RFC")
	table.insert(cfg.plistdps,"GRJ_RFC")
	table.insert(cfg.plistdps,"HMT_T")
	table.insert(cfg.plistdps,"MHZ_T")
	table.insert(cfg.plistdps,"HMT")
	table.insert(cfg.plistdps,"YHJM")
	table.insert(cfg.plistdps,"GRJ")
	--table.insert(cfg.plistdps,"XLZT")
	table.insert(cfg.plistdps,"GRJ_Force")
	table.insert(cfg.plistdps,"end")
	
	cfg.defaultdps = nil
	
	cfg.plistaoe = {}
	table.insert(cfg.plistaoe,"HYJ")
	table.insert(cfg.plistaoe,"MHZ_B_AOE")
	table.insert(cfg.plistaoe,"NLP_AOE")
	table.insert(cfg.plistaoe,"XRDST_AOE")
	table.insert(cfg.plistaoe,"SHYXT")
	table.insert(cfg.plistaoe,"end")
	
	cfg.plistrange = {}
	
	cfg.plist=cfg.plistdps
	
	
	--[[			for i=1,40 do
				local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId=UnitDebuff("target",i)
				if name then print(name.." = "..spellId.."("..count..")") end
			end]]
			
	cfg.case = {
		["CDs"] = function()
			if ns.db.cdsonboss then
				if cfg.target ~= 'worldboss' then
					return nil
				end
			end
			if cfg.spells["BHXF"] and lib.GetSpellCD("BHXF") <= lib.GetSpellCD(cfg.gcd) then
				return lib.SimpleCDCheck("BHXF")
			end
			return nil
		end,
		["RFC_Reset"] = function()
			RFC = 0
			return nil
		end,
		["XLZTR"] = function()
			if lib.GetSpellCD("XLZT") >= 24 then
				return lib.SimpleCDCheck("XLZTR")
			end
			return nil
		end,
		["HYJ"] = function()
			if lib.GetAuraNum("B_HYJ") >= 20 then
				return lib.SimpleCDCheck("HYJ")
			end
			return nil
		end,
		["HNJ"] = function()
			if cfg.power < 25 and lib.GetSpellCD("NLP") > lib.GetSpellCD(cfg.gcd) + 6 and lib.GetAura({"T_HMT"}) <= 0 then
				return lib.SimpleCDCheck("HNJ")
			end
			return nil
		end,
		["XRDST"] = function()
			if lib.GetSpellCD("XRDST") <= lib.GetSpellCD(cfg.gcd) + 0.1 then
				if cfg.chi >= 2 then
					if lib.GetAuraNum("B_HYJ") >= 17 and lib.GetSpellCD(cfg.gcd,true) < 0.5 then
						return lib.SimpleCDCheck("HYJ",0,true)
					else
						return lib.SimpleCDCheck("XRDST",0,true)
					end
				else
					RFC = cfg.spells["XRDST"].powerCost--return lib.SimpleCDCheck("GRJ",0,true)
				end
			end
			if lib.GetSpellCD("XRDST") - lib.GetSpellCD(cfg.gcd) < 1.5 then
				RFC = cfg.spells["XRDST"].powerCost
				--[[if cfg.chi < cfg.spells["XRDST"].powerCost then
					return lib.SimpleCDCheck("GRJ",0,true)
				else
					RFC = cfg.spells["XRDST"].powerCost
				end]]
			end
			return nil
		end,
		["MHZ_B"] = function()
			if lib.GetAura({"B_MHZ"}) - lib.GetSpellCD(cfg.gcd) <= 1.5 then
				if cfg.chi >= 2 then
					return lib.SimpleCDCheck("MHZ")
				else
					return lib.SimpleCDCheck("GRJ",0,true)
				end
			end
			return nil
		end,
		["NLP"] = function()
			if lib.GetSpellCD("NLP",true) <= lib.GetSpellCD(cfg.gcd) + 1.5 then
				if lib.GetSpellCD("XRDST",true) >= 3 and ((lib.GetAura({'B_MHZ'}) >= 4 + 0.5 and cfg.chi >= 1) or (lib.GetAura({'B_MHZ'}) >= 4 + 1.5)) and cfg.power <= 50 and (lib.GetAura({"B_HYJ_D"}) > 0 or lib.GetAuraNum("B_HYJ") < 16) and lib.GetAura({"HNJ"}) < 2 then
					if GetUnitSpeed('player') == 0 and lib.GetSpellCD("NLP",true) <= lib.GetSpellCD(cfg.gcd) then
						--if lib.GetAuraNum("B_HYJ") >= 10 then
						--	return lib.SimpleCDCheck("HYJ",0,true)
						--else
							return lib.SimpleCDCheck("NLP")
						--end
					else
						--return lib.SimpleCDCheck("GRJ",0,true)
						RFC = RFC + cfg.spells["NLP"].powerCost
					end
				elseif lib.GetSpellCD("XRDST",true) < 3 and lib.GetAura({'B_MHZ'}) >= lib.GetSpellCD("XRDST",true) + 5 then
					RFC = RFC + cfg.spells["NLP"].powerCost
				end
			end
			
			return nil
			--[[
			if lib.GetSpellCD("NLP") < 1.5 and cfg.chi < cfg.spells["NLP"].powerCost then
				return lib.SimpleCDCheck("GRJ",0,true)
			end]]
		end,
		["ZQB"] = function()
			if not((lib.GetSpellCD("XRDST",true) - lib.GetSpellCD(cfg.gcd) < 0.75 and cfg.chi >= 2)) and RFC == 0 and cfg.power < 86 then
				return lib.SimpleCDCheck("ZQB")
			end
		end,
		["YHJM_RFC"] = function()
			if UnitHealth('player') * 100 / UnitHealthMax("player") > 80 then
				return nil
			end
			if RFC > cfg.chi then
				return lib.SimpleCDCheck("YHJM")
			elseif RFC > 0 and cfg.chi < 4 and lib.GetSpellCD("XRDST",true) - lib.GetSpellCD(cfg.gcd) >= 0.7 then
				return lib.SimpleCDCheck("YHJM")
			elseif RFC == 5 and cfg.chi < 5 then
				return lib.SimpleCDCheck("YHJM")
			elseif cfg.power + cfg.pactiveRegen * lib.GetSpellCD(cfg.gcd,true) > 85 and lib.GetSpellCD("XRDST",true) - lib.GetSpellCD(cfg.gcd) >= 0.7 then
				return lib.SimpleCDCheck("YHJM")
			end
			return nil
		end,
		["GRJ_RFC"] = function()
			if RFC > 5 then
				RFC = 5
			end
			if RFC > cfg.chi then
				return lib.SimpleCDCheck("GRJ")
			elseif RFC > 0 and cfg.chi < 4 and lib.GetSpellCD("XRDST",true) - lib.GetSpellCD(cfg.gcd) >= 0.7 then
				return lib.SimpleCDCheck("GRJ")
			elseif RFC == 5 and cfg.chi < 5 then
				return lib.SimpleCDCheck("GRJ")
			elseif cfg.power > 90 and lib.GetSpellCD("XRDST",true) - lib.GetSpellCD(cfg.gcd) >= 0.7 and cfg.chi < 5 then
				return lib.SimpleCDCheck("GRJ")
			end
			return nil
		end,
		["HMT_T"] = function()
			if lib.GetAura({"T_HMT"}) > 0 then
				return lib.SimpleCDCheck("HMT")
			end
			return nil
		end,
		["MHZ_T"] = function()
			if lib.GetAura({"T_MHZ"}) > 0 then
				return lib.SimpleCDCheck("MHZ")
			end
			return nil
		end,
		["HMT"] = function()
			RFC = RFC or 0
			if cfg.spells["HMT"].powerCost <= cfg.chi - RFC and cfg.chi > 2 and lib.GetSpellCD("XRDST",true) - lib.GetSpellCD(cfg.gcd) >= 0.7 then
				return lib.SimpleCDCheck("HMT")
			end
			return nil
		end,
		["YHJM"] = function ()
			if UnitHealth('player') * 100 / UnitHealthMax("player") > 80 then
				return nil
			end
			if not((lib.GetSpellCD("XRDST",true) - lib.GetSpellCD(cfg.gcd) < 0.75 and cfg.chi >= 2) or (lib.GetSpellCD("NLP",true) - lib.GetSpellCD(cfg.gcd) < 0.75 and cfg.chi >= 3) or (cfg.chi == 4 and cfg.power <= 90) or (cfg.chi == 5 and cfg.power <= 100)) then
				return lib.SimpleCDCheck("YHJM")
			end
			return nil
		end,
		["GRJ"] = function()
			if not((lib.GetSpellCD("XRDST",true) - lib.GetSpellCD(cfg.gcd) < 0.75 and cfg.chi >= 2) or (lib.GetSpellCD("NLP",true) - lib.GetSpellCD(cfg.gcd) < 0.75 and cfg.chi >= 3) or (cfg.chi == 4 and cfg.power <= 90) or (cfg.chi == 5 and cfg.power <= 100) ) then
				return lib.SimpleCDCheck("GRJ")
			end
			return nil
		end,
		["XLZT"] = function()
			return lib.SimpleCDCheck("XLZT")
		end,
		
		["GRJ_Force"] = function()
			if not((lib.GetSpellCD("XRDST",true) - lib.GetSpellCD(cfg.gcd) < 0.75 and cfg.chi >= 2) or (lib.GetSpellCD("NLP",true) - lib.GetSpellCD(cfg.gcd) < 0.75 and cfg.chi >= 3) or (cfg.chi == 4 and cfg.power <= 90) or (cfg.chi == 5 and cfg.power <= 100)) then
				return lib.SimpleCDCheck("GRJ",0,true)
			end
			return nil
		end,
		
		["XRDST_AOE"] = function()
			if lib.GetSpellCD("XRDST") <= lib.GetSpellCD(cfg.gcd) then
				if cfg.chi >= 5 then
					return lib.SimpleCDCheck("XRDST")
				end
			end
			return nil
		end,
		["MHZ_B_AOE"] = function()
			if lib.GetAura({"B_MHZ"}) - lib.GetSpellCD(cfg.gcd) <= 2 then
				return lib.SimpleCDCheck("MHZ")
			end
			return nil
		end,
		["NLP_AOE"] = function()
			if lib.GetSpellCD("NLP") <= lib.GetSpellCD(cfg.gcd) then
				if ((lib.GetAura({'B_MHZ'}) >= 4 + 0.5 and cfg.chi >= 1) or (lib.GetAura({'B_MHZ'}) >= 4 + 1.5)) and cfg.power <= 60 and GetUnitSpeed('player') == 0 then
					return lib.SimpleCDCheck("NLP") 
				end
			end
			
			return nil
			--[[
			if lib.GetSpellCD("NLP") < 1.5 and cfg.chi < cfg.spells["NLP"].powerCost then
				return lib.SimpleCDCheck("GRJ",0,true)
			end]]
		end,
		["SHYXT"] = function()
			return lib.SimpleCDCheck("SHYXT",0,true)
		end,
	}
	
	cfg.gcd="GRJ"
	cfg.mode = "dps"
	lib.onclick = function()
		if cfg.mode == "dps" then
			cfg.mode = "aoe"
			cfg.plist=cfg.plistaoe
			cfg.Update=true
		else
			cfg.mode = "dps"
			cfg.plist=cfg.plistdps
			lib.changeDPSMode()
			cfg.Update=true
		end
	end
	
	return true
end

lib.classevents["MONK"] = function()
	lib.ChiPowerUpdate() --cfg.chi
	cfg.maxchi=UnitPowerMax("player",SPELL_POWER_CHI)
	lib.myonpower = function(unit,powerType)
		if unit=="player" and powerType=="CHI" then
--			print("CHI "..UnitPower("player" , SPELL_POWER_LIGHT_FORCE))
			if lib.ChiPowerUpdate() then
				cfg.Update=true
			end
		end
	end
	
	lib.myonequip=function()
	end
	lib.myonequip()
	
	lib.rangecheck=function()
		if lib.inrange("Jab") then
			lib.bdcolor(Heddframe.bd,nil)
		else
			lib.bdcolor(Heddframe.bd,{1,0,0,1})
		end
	end
	
	local playerhealth,playerhealthmax,playerhealthperc
	lib.myonhealthupdate=function(unit)
		if unit=="player" then
			playerhealth=cfg.phealth
			playerhealthmax=cfg.phealthmax
			playerhealthperc=cfg.phealthperc
			cfg.phealthmax=UnitHealthMax("player")
			cfg.phealth=UnitHealth("player")
			cfg.phealthperc=(cfg.phealthmax==0) and 100 or hedlib.round(cfg.phealth*100/cfg.phealthmax,0)
			if cfg.phealth~=playerhealth or cfg.phealthmax~=playerhealthmax or cfg.phealthperc~=playerhealthperc then
				cfg.Update=true
			end
		end
	end
	lib.myonhealthupdate("player")
end